package com.coder.algorithm.leetcode;

/**
 * [34. 在排序数组中查找元素的第一个和最后一个位置](https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array/)
 *
 * @author yuhushuan
 * @date 2020/10/20 00:10
 */
class l_0034 {

    public int[] searchRange(int[] nums, int target) {
        if(nums == null){
            return new int[]{-1,-1};
        }
        int l = 0, r = nums.length - 1;
        while (l<r){
            int mid = l + ((r - l) >> 1);
            if (nums[mid] >= target) {//left
                r = mid;
            }else {
                l = mid + 1;
            }
        }
        int start = l;
        if (nums[l] != target){//不存在
            return new int[]{-1,-1};
        }
        l = 0;
        r = nums.length - 1;
        while (l < r) {
            int mid = l + ((r - l  + 1) >> 1);// right
            if (nums[mid] <= target) {// right
                l = mid;
            } else {
                r = mid - 1;
            }
        }
        int end = l;
        return new int[]{start, end};
    }
}
